
void test_logger() 
{
    info("%s-%d", "测试", 1);
    debug("%s-%d", "测试", 1);
    warning("%s-%d", "测试", 1);
    error("%s-%d", "测试", 1);
    fatal("%s-%d", "测试", 1);
}

int test_mysql_util()
{
    MYSQL *mysql = mysql_util::mysql_init_handle(HOST, PORT, USER, PASSWORD, "gobang");
    if(mysql == NULL) return -1;
    if(!mysql_util::mysql_exe(mysql,"insert into test_stu (name, age, chinese, english, math) values ('刘备', 9, 100, 100, 100);")) 
        return -2;
    mysql_util::mysql_destory(mysql);
    return 0;
}

void test_josn_util()
{
    Json::Value root;
    root["name"] = "zhangsan";
    root["sex"] = "nan";
    root["age"] = 18;
    std::string json_string;
    json_util::serialize(root, json_string);
    std::cout << json_string << std::endl;

    Json::Value leaf;
    json_util::deserialize(json_string, leaf);
    std::cout << leaf["name"].asString() << std::endl;
    std::cout << leaf["sex"].asString() << std::endl;
    std::cout << leaf["age"].asInt() << std::endl;
}

void test_string_util()
{
    std::string src = ",,,...,123,234,345,,,,456,";
    std::string sep = ",";
    std::vector<std::string> res;
    string_util::split(src, sep, res);
    for(auto s : res) {
        debug("%s", s.c_str());
    }
}

void test_file_open()
{
    std::string filename = "./gobang.cc";
    std::string body;
    file_util::read(filename, body);

    std::cout << body << std::endl;
}

void test_db()
{
    User_Info_Table user(HOST, PORT, USER, PASSWORD, "gobang");
    Json::Value root;
    root["username"] = "ovo";
    root["password"] = "123456";
    user.insert(root);
    //user.win(3);
    //user.lose(3);
    //Json::Value root;
    //std::string str;
    //user.select_by_id(1, root);
    //json_util::serialize(root, str);
    //std::cout << str << std::endl;
    //Json::Value root;
    //root["username"] = "张三";
    //root["password"] = "123456";
    //if(user.login(root)) std::cout << "login success" << std::endl;
    //else std::cout << "login fail" << std::endl;
}

void test_online_manager()
{
    online_user_manager manager;
    ws_server_t::connection_ptr conn;
    manager.enter_game_room(1, conn);
    if(manager.is_in_game_room(1)){
        std::cout << "in hall";
    }
    else {
        std::cout << "not in hall";
    }
    manager.exit_game_room(1);
    if(manager.is_in_game_room(1)){
        std::cout << "in hall";
    }
    else {
        std::cout << "not in hall";
    }
}

void test_room()
{
    User_Info_Table user(HOST, PORT, USER, PASSWORD, "gobang");
    online_user_manager manager;
    room r(1, &user, &manager);
}

void test_room_manager()
{
    User_Info_Table user(HOST, PORT, USER, PASSWORD, "gobang");
    online_user_manager ou;
    room_manager manager(&user, &ou);
    auto room = manager.create_room(1,2);
}

void test_session()
{
    User_Info_Table user(HOST, PORT, USER, PASSWORD, "gobang");
    online_user_manager ou;
    session_manager manager(nullptr);
}

void test_matcher()
{
    User_Info_Table user(HOST, PORT, USER, PASSWORD, "gobang");
    online_user_manager ou;
    room_manager manager(&user, &ou);
    matcher ma(&manager, &user, &ou);
}

void test_add_friend()
{
    User_Info_Table user(HOST, PORT, USER, PASSWORD, "gobang");
    //Json::Value root;
    //root["username"] = "1";
    //root["password"] = "123456";
    //user.insert(root);
    //root["username"] = "2";
    //root["password"] = "123456";
    //user.insert(root);
    //root["username"] = "3";
    //root["password"] = "123456";
    //user.insert(root);

    //user.select_by_username("1", root);
    //std::cout << root["id"] << " " << root["ladder"] << " " << root["rank"] << " " << root["score"] << " " <<
    //root["total_count"] << " " << root["win_count"] << " " << root["last_active"] << " " << root["status"] << " " 
    //<< root["session_id"] << std::endl;
    //user.select_by_username("2", root);
    //std::cout << root["id"] << " " << root["ladder"] << " " << root["rank"] << " " << root["score"] << " " <<
    //root["total_count"] << " " << root["win_count"] << " " << root["last_active"] << " " << root["status"] << " " 
    //<< std::endl;
    //user.select_by_username("3", root);
    //std::cout << root["id"] << " " << root["ladder"] << " " << root["rank"] << " " << root["score"] << " " <<
    //root["total_count"] << " " << root["win_count"] << " " << root["last_active"] << " " << root["status"] << " " 
    //<< std::endl;
    //user.select_by_id(1, root);
    //std::cout << root["username"] << " " << root["ladder"] << " " << root["rank"] << " " << root["score"] << " " <<
    //root["total_count"] << " " << root["win_count"] << " " << root["last_active"] << " " << root["status"] << " " 
    //<< root["session_id"] << std::endl;
    //user.select_by_id(2, root);
    //std::cout << root["username"] << " " << root["ladder"] << " " << root["rank"] << " " << root["score"] << " " <<
    //root["total_count"] << " " << root["win_count"] << " " << root["last_active"] << " " << root["status"] << " " 
    //<< std::endl;
    //user.select_by_id(3, root);
    //std::cout << root["username"] << " " << root["ladder"] << " " << root["rank"] << " " << root["score"] << " " <<
    //root["total_count"] << " " << root["win_count"] << " " << root["last_active"] << " " << root["status"] << " " 
    //<< std::endl;
    //user.add_friend(1, 2);
    //user.add_friend(1, 3);
    //user.add_friend(2, 1);
    //std::vector<int> ids;
    //user.select_friend_id_by_id(1, ids);

    //Json::Value root;
    //for(auto id : ids) {
    //    user.select_by_id(id, root);
    //    std::cout << root["username"] << " " << root["ladder"] << " " << root["rank"] << " " << root["score"] << " " <<
    //    root["total_count"] << " " << root["win_count"] << " " << root["last_active"] << " " << root["status"] << " " 
    //    << root["session_id"] << std::endl;
    //}
    // 请求好友列表
    Json::Value resp_json;
    resp_json["optype"] = "get_friend_list";
    resp_json["result"] = true;
    resp_json["data"] = Json::Value(Json::arrayValue); 

    // 获取所有好友id
    std::vector<int> ids;
    user.select_friend_id_by_id(1, ids);

    // 获取所有好友id的具体信息
    Json::Value info;
    for(auto id : ids) {
        user.select_by_id(id, info);
        resp_json["data"].append(info);
    }
    std::string str;
    json_util::serialize(resp_json, str);
    std::cout << str << std::endl;
}

int main()
{
    //test_add_friend();
    //test_db();
    //test_online_manager();
    //test_room();
    //test_room_manager();
    //test_session();
    //test_matcher();
    return 0;
}